package com.tunnel.common.config;

import com.tunnel.common.config.client.ClientConfig;
import com.tunnel.common.config.client.ProxyClientConfig;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClientParsConfig implements ParsConfig<ClientConfig> {
   public static ClientConfig ClientConfig = new ClientConfig();
    private String configPath = "client-config.yml";

    @Override
    public  ClientConfig parse() {
        // 加载 YAML 文件
        InputStream inputStream = ClientParsConfig.class.getClassLoader().getResourceAsStream(configPath);
        // 创建 Yaml 对象
        Yaml yaml = new Yaml();
        // 解析 YAML 文件
        Map<String, HashMap<String, Object>> data = yaml.load(inputStream);
        String key = (String) data.get("local").get("key");
        String remoteIp = (String) data.get("local").get("remoteIp");
        int remotePort = (Integer) (data.get("local").get("remotePort"));
        String clientName = (String) data.get("local").get("clientName");

        List<ProxyClientConfig> proxyConfigs = new ArrayList<>();
        List<Map<String, String>> proxyList = (List<Map<String, String>>) data.get("proxy");
        proxyList.stream().forEach(item -> {
            ProxyClientConfig proxyConfig = new ProxyClientConfig();
            proxyConfig.setName(item.get("name"));
            proxyConfig.setIp(item.get("ip"));
            proxyConfig.setPort(String.valueOf(item.get("port")));
            proxyConfig.setProtocol(item.get("protocol"));
            proxyConfigs.add(proxyConfig);
        });
        ClientConfig.setClientName(clientName);
        ClientConfig.setKey(key);
        ClientConfig.setRemoteIp(remoteIp);
        ClientConfig.setRemotePort(remotePort);
        ClientConfig.setProxyConfigs(proxyConfigs);
        System.out.println("解析成功");
        return ClientConfig;
    }


}
